<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // const arraypro = Array.prototype;
        // const arraymethod = Object.create(arraypro);
        // const newArr = [];
        // ['push', 'pop'].forEach((method) => {
        //     let res = arraymethod[method];
        //     newArr[method] = function() {
        //         console.log('监听到了');
        //         res.apply(this, arguments);
        //     }
        // })
        // let arr = [1, 2, 3];
        // arr.__proto__ = newArr;
        // arr.push(4);
        // //ES6实现
        class NewArray extends Array {
            constructor(...arg) {
                super(...arg);
            }
            push(...arg) {
                console.log('监听到了');
                return super.push(...arg);
            }
        }

        let list3 = [1, 2];

        let arr = new NewArray(...list3);
        console.log(arr)
            // (2) [1, 2]

        arr.push(3);
        // 监听到数组的变化啦！
        console.log(arr)
            // (3) [1, 2, 3]
    </script>
</body>

</html>